package com.lagou.zkClient;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @author
 */
public class ZkClientFactory {

    private static final String CONNECT_STRING = "39.100.105.145:2181";

    private static CuratorFramework zkClient;

    /**
     * 创建一个zk客户端
     * @return
     */
    public static CuratorFramework getZkClient(){
        if(zkClient == null){
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
            zkClient = getZkClient(60000, retryPolicy);
        }
        return zkClient;
    }

    /**
     * 获取客户端
     * @return
     */
    public static CuratorFramework getZkClient(int sessionTimeOut, RetryPolicy retryPolicy){
        int connectionTimeOutMs = 15000;
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(CONNECT_STRING, sessionTimeOut, connectionTimeOutMs,retryPolicy);
        curatorFramework.start();
        System.out.println("Zookeeper session established. ");
        return curatorFramework;
    }
}
